import React, { Component } from 'react';
import { Form, Radio, Button, message} from 'antd';
import { messages } from 'share/common';
import DevelopmentPlatformService from './development-platform.service';
const FormItem = Form.Item;
const RadioGroup = Radio.Group;

@Form.create()

class Lark extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      setting: {}
    }
  }

  componentWillMount() {

  }

  componentDidMount() {
    this.getSetting();
  }

  getSetting = () => {
    DevelopmentPlatformService.getSetting()
      .then((res) => {
        this.setState({
          setting: res.data,
        })
      })
  };

  saveSetting = () => {
    this.setState({
      loading: true,
    });
    this.props.form.validateFieldsAndScroll((err, values) => {
      if(!err){
        DevelopmentPlatformService.saveSetting(values)
          .then((res) => {
            this.getSetting();
            this.setState({
              loading: false,
            });
            message.success(messages('basic-12.key1')/*保存成功*/);
          }).catch(err => {
          this.setState({
            loading: false,
          })
        })
      }
    })
  };

  render() {
    const { loading, setting } = this.state;
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: {span: 5},
      wrapperCol: {span: 8, offset: 1},
    };
    const { disabled } = this.props;
    return (
      <div className="left-edge-gap" style={{width: 860}}>
        <div className="bottom-gap">{messages('basic-12.key19')/*设置说明:  基于飞书平台相关的功能设置，应用范围全租户*/}</div>
        <Form className="new-form">
          <div className="standard-divider-title">{messages('basic-12.key3')/*工作流设置*/}</div>
          <FormItem {...formItemLayout} label={messages('basic-12.key20')/*对接飞书审批*/}>
            {getFieldDecorator('feishuWorkflow',{
              initialValue: setting.feishuWorkflow || false
            })(
              <RadioGroup>
                <Radio value={true}>{messages('common.status.enable')/*启用*/}</Radio>
                <Radio value={false}>{messages('basic-12.key5')/*不启用*/}</Radio>
              </RadioGroup>
            )}
            <div>{messages('basic-12.key21')/*若启用，可在飞书的审批应用中操作审批流程*/}</div>
          </FormItem>
          <Button type="primary"
                  disabled={disabled}
                  onClick={this.saveSetting}
                  style={{marginBottom: 20}}
                  loading={loading}>
            {messages('common.save')/*保存*/}
          </Button>
        </Form>
      </div>
    );
  }
}

export default Lark;
